/**
 * 
 */
package com.yst.metho.web.application.projectversion.views;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.CreateException;
import javax.ejb.FinderException;
import javax.naming.NamingException;

import nextapp.echo2.app.Component;
import nextapp.echo2.app.Extent;
import nextapp.echo2.app.SelectField;
import nextapp.echo2.app.event.ActionEvent;
import nextapp.echo2.app.event.ActionListener;

import com.yst.metho.web.application.interfaces.IListingViewModel;
import com.yst.metho.web.application.projectversion.actions.lot.CreateLotAction;
import com.yst.metho.web.application.projectversion.actions.lot.EditLotAction;
import com.yst.metho.web.application.projectversion.renderer.ProjectVersionListRenderer;
import com.yst.metho.web.application.utils.AbstractSuiviComparator;
import com.yst.metho.web.application.utils.AbstractSuiviTableModel;
import com.yst.metho.web.application.utils.Locator;
import com.yst.suivi.data.project.Project;
import com.yst.suivi.data.project.versions.ProjectVersion;
import com.yst.suivi.data.project.versions.lot.Lot;

/**
 * @author perso
 * 
 */
public class ProjectLotListModel extends AbstractSuiviTableModel implements IListingViewModel, ActionListener
{
	private class ProjectLotComparator extends AbstractSuiviComparator
	{

		/*
		 * (non-Javadoc)
		 * 
		 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
		 */
		public int compare(Object o1, Object o2)
		{
			Lot u1 = (Lot) o1;
			Lot u2 = (Lot) o2;
			int comparaison = 0;
			switch (getColumn())
			{
			case 0:
				comparaison = u1.getNom().compareToIgnoreCase(u2.getNom());
				break;
			default:
				break;
			}
			if (!isAscending())
			{
				comparaison = -1 * comparaison;
			}
			return comparaison;
		}

	}

	private String[] mColumnsName =
	{ "Nom" };

	// private Collection mActivitiesList;

	private ProjectVersion mSelectedPrjVersion;

	private SelectField mVersionList;

	private ProjectVersionListModel mPrjVersionListModel;

	private Project mPrj;

	public ProjectLotListModel(ProjectVersionListModel prjVersionListModel, Project prj) throws NamingException, FinderException, CreateException
	{
		super();
		mPrjVersionListModel = prjVersionListModel;
		mPrj = prj;
		setComparator(new ProjectLotComparator());
		refresh();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.yst.metho.web.application.interfaces.IListingViewModel#getToolbarAction()
	 */
	public List getToolbarAction()
	{
		ArrayList actions = new ArrayList();
		actions.add(new CreateLotAction(this, mVersionList));
		actions.add(new EditLotAction(this, mVersionList, mPrj));
		return actions;
	}

	public Component getVersionChoiceBox()
	{
		mVersionList = new SelectField();
		mVersionList.setWidth(new Extent(80, Extent.PX));
		mVersionList.setCellRenderer(new ProjectVersionListRenderer());
		mVersionList.setModel(mPrjVersionListModel);
		mVersionList.addActionListener(this);
		return mVersionList;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.yst.metho.web.application.interfaces.IListingViewModel#refresh()
	 */
	public void refresh()
	{
		try
		{
			if (null != mVersionList)
			{
				if (!mVersionList.getSelectionModel().isSelectionEmpty())
				{
					mList = Locator.getLotView().findLot(mSelectedPrjVersion.getId());
					fireTableDataChanged();
				}
			}
		}
		catch (NamingException e)
		{
			throw new RuntimeException(e);
		}
		catch (FinderException e)
		{
			throw new RuntimeException(e);
		}
		catch (CreateException e)
		{
			throw new RuntimeException(e);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see nextapp.echo2.app.table.AbstractTableModel#getColumnName(int)
	 */
	public String getColumnName(int arg0)
	{
		return mColumnsName[arg0];
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see nextapp.echo2.app.table.TableModel#getColumnCount()
	 */
	public int getColumnCount()
	{
		return mColumnsName.length;
	}

	public void actionPerformed(ActionEvent e)
	{
		if (-1 != mVersionList.getSelectedIndex())
		{
			int selectedIndex = mVersionList.getSelectionModel().getMinSelectedIndex();
			mSelectedPrjVersion = (ProjectVersion) mPrjVersionListModel.get(selectedIndex);
			refresh();
		}
	}

	/**
	 * @return the prj
	 */
	public Project getPrj()
	{
		return mPrj;
	}

	/**
	 * @return the prjVersionListModel
	 */
	public ProjectVersionListModel getPrjVersionListModel()
	{
		return mPrjVersionListModel;
	}
}
